<?php
#注册插件
RegisterPlugin("Xd_ad_Article","ActivePlugin_Xd_ad_Article");

function ActivePlugin_Xd_ad_Article() {
Add_Filter_Plugin('Filter_Plugin_Edit_Response','Xd_ad_Article_ad');
Add_Filter_Plugin('Filter_Plugin_ViewPost_Template','Xd_ad_Article_ad_kt');    
} 

function Xd_ad_Article_SubMenu($id){
	$arySubMenu = array(
		0 => array('基本配置', 'jbsz', 'left', false),
	);
	foreach($arySubMenu as $k => $v){
		echo '<li><a href="?act='.$v[1].'" '.($v[3]==true?'target="_blank"':'').' class="'.($id==$v[1]?'on':'').'">'.$v[0].'</a></li>';
	}
} 	
function Xd_ad_Article_ad(){ 
global $zbp,$article;
	echo '
<div id="alias" class="editmod">
<label for="meta_adintr" class="editinputname">广告代码--此处为空时，文章页前台不显示；</label>
<textarea type="text" style="width: 98%;margin:5px;height: 60px;" name="meta_adintr" >'.htmlspecialchars($article->Metas->adintr).'</textarea>
</div>';
}

function Xd_ad_Article_ad_kt(&$template){ 
global $zbp;
    $article = $template->GetTags('article');
    if($article->Type==ZC_POST_TYPE_ARTICLE){
        if(!empty($article->Metas->adintr)){ 
	$hdpab = $article->Metas->adintr;
            } 
}
        if($zbp->Config('Xd_ad_Article')->adweizhixz == "d"){
        }
        if($zbp->Config('Xd_ad_Article')->adweizhixz == "a"){
			  $article->Content=$hdpab.$article->Content;
        }
		if($zbp->Config('Xd_ad_Article')->adweizhixz == "b"){
			  $article->Content=$article->Content.$hdpab;
		}
        if($zbp->Config('Xd_ad_Article')->adweizhixz == "c"){ 
	          $adweizhis = $zbp->Config('Xd_ad_Article')->adweizhi;  
	          $Content= Xd_ad_Article_ad_neir( $hdpab, $adweizhis, $article->Content );  
			  $article->Content = $Content; 
	    }
	$template->SetTags('article', $article);
}       	  	

// 插入广告所需的内容代码     	  	 	 
function Xd_ad_Article_ad_neir( $adneir, $weizhi_id, $content ) {     	  		
// 文章中查找p     	 	   	 	
	$weiz_p = '</p>';    
	$pweizshuzus = explode( $weiz_p, $content );      	
// 循环在原来 p 位置插入内容为广告      	 		
	foreach ($pweizshuzus as $index => $pweizshuzu) {    	   				
		if ( trim( $pweizshuzu ) ) {    	  			 	
			$pweizshuzus[$index] .= $weiz_p;    		  		 	
		}    		      
		if ( $weizhi_id == $index + 1 ) {        	   
			$pweizshuzus[$index] .= $adneir;    		  		 	
		}        			 
	}       	  	  	
// 显示文章（全部文章+原P位置插入的广告内容）     	 		
	return implode( '', $pweizshuzus );     			 		 
}   

function InstallPlugin_Xd_ad_Article() {}
function UninstallPlugin_Xd_ad_Article() {}